// xc_lshop/integralmall/my/integral.js
var actbill = require('../model/integral_exchange')
var app = getApp()

Page({
  mixins: [require('../../../xc/mixins/basicMixin')],

  /**
   * 页面的初始数据
   */
  data: {
    keyval: "integral", //amount余额 integral积分
    op: 'into',
    value: "",
    shoptext:"商城积分",
    wqtext:"微擎积分"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  xcloaddata: function (options, config) {
    var that = this;
    if (options.key && options.key != this.data.keyval) {
      var update={};
      update['keyval']= options.key;
    
      if(options.key=='integral'){
        update['shoptext']='商城积分';
        update['wqtext']='微擎积分';
      }else{
        update['shoptext']='商城余额';
        update['wqtext']='微擎余额';
      }
      this.setData(update)
    }
    this.setconfig(config);
    this.getwqintegral()
    return;
    if (config.integral && config.integral[key]) {

      if (!config.integral.exchange.ratestatus) {
        this.error("积分转换配制未开启", true)
      } else {
        if (this.data.keyval == 'integral') {
          var op = config.integral.exchange.ratestatus[0];
          this.setData({
            op: op,
            exchangeconfig: config.integral.exchange
          })
        } else {
          var op = config.integral.exmoney.ratestatus[0];
          this.setData({
            op: op,
            exchangeconfig: config.integral.exmoney
          })
        }

      }

    }

  },
  setconfig(config) {
    if (!config.integral) {
      this.error("积分商城未配制", true);
      return;
    }
    if (this.data.keyval == 'integral') {
      var integralconfig = config.integral['exchange'];
    } else {
      var integralconfig = config.integral['exmoney'];
    }
   
    if (!integralconfig.ratestatus) {
      this.error("转换配制未开启", true)
    } else {
      var op = integralconfig.ratestatus[0];
      this.setData({
        wqtext:integralconfig.wqtext,
        shoptext:integralconfig.shoptext,
        op: op,
        exchangeconfig: integralconfig
      })


    }
  },
  getwqintegral() {
    var that = this;
    actbill.getwqint().then(res => {
  
      that.setData({
        wq: res['obj']
      })
    })
  },
  inputval(e) {
    console.log(e)
    var val = e.detail.value;
    this.changeval(val)
  },
  changeval(val) {
    var op = this.data.op;
    if (this.data.keyval == 'integral') {
    
      if (op == 'out') {
      
        if ((val * 1) > (this.data.member.memberinfo.score * 1)) {
          val = this.data.member.memberinfo.score
        }
        var exchangeint = this.data.exchangeconfig.rateout * 0.01 * val;
      } else {
        console.log('3333')
        if ((val * 1) > (this.data.wq.wqintegral * 1)) {
          val = this.data.wq.wqintegral
        }
        var exchangeint = this.data.exchangeconfig.rateinto * 0.01 * val;
      }
      val = parseInt(val);

  

      exchangeint = parseInt(exchangeint);
    
    }else{
     

      if (op == 'out') {
        console.log(val * 100 > this.data.member.memberinfo.amount)
        if ((val * 100) > (this.data.member.memberinfo.amount * 1)) {
          val = (this.data.member.memberinfo.amount / 100).toFixed(2)
        }
        var exchangeint = this.data.exchangeconfig.rateout * 0.01 * val;
      } else {
        if ((val * 1) > (this.data.wq.wqmoney * 1)) {
          val = this.data.wq.wqmoney
        }
        var exchangeint = this.data.exchangeconfig.rateinto * 0.01 * val;
      }
    

      exchangeint = exchangeint.toFixed(2);
     
    }

    this.setData({
      exchangeint: exchangeint,
      value: val
    })
  },
  exchange() {
    var that = this;
    var value = this.data.value;
    if(value<=0){
      wx.showToast({
        icon:'none',
        title:'值不小于0',
      })
      return;
    }
    var op = this.data.op;
    if (value) {
      var name=this.data.keyval=="integral"?this.data.integralname:'余额'
      var exchangeint = this.data.exchangeint;
      if (op == 'out') {
        var content = "确认将" + value + "商城"+name+"转换为" + exchangeint + "微擎"+name;
      } else {
        var content = "确认将" + value + "微擎"+name+"转换为" + exchangeint + "商城"+name;
      }
      wx.showModal({
        title: "确认转换",
        content: content,
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
            that.opexchange()
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  },
  opexchange() {
    var that = this;
    var model = {
      keyval: this.data.keyval,
      op: this.data.op,
      value: this.data.value,
      exchangeint: this.data.exchangeint,
    }
    actbill.exchange(model).then(res => {

      if (res['status'] == 1) {
        wx.showToast({
          title: '转换成功',
        })
        app.globalData.member = res['obj']['member']
        that.setData({
          value: "",
          exchangeint: "",
          member: res['obj']['member']
        })
        that.getwqintegral()
      }
    })
  },
  all(e) {
    console.log(e)
    var val = e.currentTarget.dataset.val
    this.setData({
      value: val
    })
    this.changeval(val)
  },
  afterlogin: function (options, config, userinfo) {
    var that = this;
    this.setData({
      member: userinfo
    })
    if (userinfo['member_id'] == 0) {
      this.login()
    }
  },
  // 登录注册
  login(e) {
    var that = this;
    app.xc_sys_config.authop.oppower("login").then(function (res) {
      that.setData({
        "member": app.globalData.member
      })
    })
  },
  into() {
    this.setData({
      op: 'into',
      value: "",
      exchangeint: "",
    })
  },
  out() {
    this.setData({
      op: 'out',
      value: "",
      exchangeint: "",
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  
})